#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

#include "outputAPI.h"

//#define F_OFF _off64_t
#ifdef WINDOWS
    /*#define F_OFF off64_t      */
	#ifdef __MINGW32__
		#define F_OFF _off64_t
	#else
    	#define F_OFF __int64
	#endif
#else
    #define F_OFF off_t
#endif
// define fseeko64 in different platform
#ifdef WINDOWS
    #ifdef __MINGW32__
       int (*fseeko64_)(FILE*, F_OFF, int) = &fseeko64;
    #else
        int (*fseeko64_)(FILE*, F_OFF, int) = &_fseeki64;
    #endif
#else
     int (*fseeko64_)(FILE*, F_OFF, int) = &fseeko;
#endif

int main(int argc, char* argv[]){
	
// 目的：读取out文件中指定点的时间序列结果
// argv1：输出文件路径
// argv2：节点名称
// argv3：节点属性
	// 输出文件路径
	char path[MAXFILENAME];
	strcpy(path, argv[1]);


	printf("test output file path: %s\n",path);

	// 处理动态库文件
	// 导入库文件
	HMODULE module = LoadLibrary("libswmmoutputapi.dll");
	if (module == NULL){
		printf("Failed to load dll\n");
	}
	// 定义SMO_open函数，并指向dll中的对应函数，打开文件
	typedef int(*dll_SMO_open)(SMOutputAPI*, const char*);
	dll_SMO_open SMO_open =
		(dll_SMO_open)GetProcAddress(module, "SMO_open");
	// 定义SMO_getProjectSize函数，并指向dll中的对应函数，获取工程大小
	typedef int(*dll_SMO_getProjectSize)(SMOutputAPI* smoapi,
		    SMO_elementCount code, int* count);
	dll_SMO_getProjectSize SMO_getProjectSize =
		(dll_SMO_getProjectSize)GetProcAddress(module, "SMO_getProjectSize");
	// 定义SMO_getNodeSeries函数，读取时间序列结果
	typedef int(*dll_SMO_getNodeSeries)(SMOutputAPI* smoapi, int nodeIndex, SMO_nodeAttribute attr,
					    long timeIndex, long length, float* outValueSeries);
	dll_SMO_getNodeSeries SMO_getNodeSeries =
		(dll_SMO_getNodeSeries)GetProcAddress(module, "SMO_getNodeSeries");
	// SMO_getUnits，获取单位
	typedef int(*dll_SMO_getUnits)(SMOutputAPI* smoapi, SMO_unit code, int* unitFlag);
	dll_SMO_getUnits SMO_getUnits =
		(dll_SMO_getUnits)GetProcAddress(module, "SMO_getUnits");

	//
	// 开始读取 -------------------------------------------------------------------------------
	// 初始化文件指针
	SMOutputAPI* smoapi = (SMOutputAPI*)malloc(sizeof(SMOutputAPI)); 
	// 打开输出文件
	// --- open the output file
	int errBEM = SMO_open(smoapi, path);
	printf("err code: %i\n", errBEM);

	// 读取时间序列结果
	// SMO_getNodeSeries(smoapi, 1, 0, )；
	// 关闭文件
	SMO_close(smoapi);
	// SMO_getNodeSeries
	FreeLibrary(module);

	return 0;
}
